package br.ufrj.ppgi.jemf_advanced_example.witness;

import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ScrollView;
import br.ufrj.ppgi.jemf.mobile.bean.Witness;
import br.ufrj.ppgi.jemf.mobile.view.WitnessView;
import br.ufrj.ppgi.jemf.utils.CustomConversions;
import br.ufrj.ppgi.jemf.utils.CustomFormat;
import br.ufrj.ppgi.jemf_advanced_example.BuildConfig;

/**
 * A fragment representing a single Witness detail screen.
 * This fragment is either contained in a {@link WitnessListActivity} in two-pane mode (on tablets) or a {@link WitnessDetailActivity} on handsets.
 */
public class WitnessDetailFragment extends Fragment {
    /**
     * The serialization (saved instance state) Bundle key representing the activated item position.
     * Only used on tablets.
     */
    private static final String STATE_WITNESS = "activated_witness";
	/**
     * The fragment argument representing the item ID that this fragment represents.
     */
    public static final String ARG_ID = "WITNESS";
    /**
     * The Witness content this fragment is showing.
     */
    private static Witness witness;
    /**
     * The Witness view holder.
     */
    private static WitnessView witnessView;



    /**
     * Mandatory empty constructor for the fragment manager to instantiate the fragment (e.g. upon screen orientation changes).
     */
    public WitnessDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the Witness content specified by the fragment arguments.
        if (getArguments().containsKey(WitnessDetailFragment.ARG_ID)) {
        	witness = getArguments().getParcelable(WitnessDetailFragment.ARG_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	    // Show the Witness content
	    if (witness != null) {
	    	witnessView = new WitnessView(getActivity(), false);
	    	// Name
	    	witnessView.setWitnessNameText(witness.getName());
	    	// Gender
	    	witnessView.setWitnessGenderBool(CustomFormat.IntToBool(witness.getGender()));
	    	// Birth Date
	    	witnessView.setWitnessBirthDateText(witness.getBirthDateString());
	    	// Age
	    	witnessView.setWitnessAgeText(String.valueOf(witness.getAge()));

	    	// Its necessary to add the WitnessView inside a ScrollView to show scrolls
	    	ScrollView scrollView = new ScrollView(getActivity());
	    	scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	    	// Add Witness View as a child of the ScrollView.
	    	scrollView.addView(witnessView);
	    	// Add a Tag to the view location
	    	scrollView.setTag(ARG_ID);

			return scrollView;
    	} else {
        	return null;
    	}
    }

    @Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
        // Restore the previously serialized activated Witness
        if (savedInstanceState != null && savedInstanceState.containsKey(STATE_WITNESS)) {
        	witness = savedInstanceState.getParcelable(STATE_WITNESS);
        }
	}





	/**
     * Get updated Witness
     * @return Witness
     */
	public static Witness getWitness() {
		if (BuildConfig.DEBUG) Log.i(WitnessListActivity.TAG, "WitnessDetailFragment ::: getWitness");
    	return witness;
    }

	/**
	 * Update Witness data from UI components
	 */
	public static void updateDataFromUI() {
    	if ((witness != null) && (witnessView != null)) {
    		if (BuildConfig.DEBUG) Log.i(WitnessListActivity.TAG, "WitnessDetailFragment ::: updateDataFromUI");
    		// Update Witness object with new data included by user through components
    		witness.setName(witnessView.getWitnessNameText());
    		witness.setGender(CustomFormat.BoolToInt(witnessView.getWitnessGenderBool()));
    		witness.setBirthDateString(witnessView.getWitnessBirthDateText());
    		witness.setAge(Integer.valueOf(witnessView.getWitnessAgeText()));
    		if (witnessView.getWitnessAgeText().isEmpty()) {
    			witness.setAge(0);
    		} else {
    			witness.setAge(CustomConversions.birthDateToAge(witness.getBirthDate()));
    	    	// Updating Age TO UI.
        		witnessView.setWitnessAgeText(String.valueOf(witness.getAge()));
    		}
        }
    }



    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Force update data from UI
        updateDataFromUI();
        // Serialize and persist the activated Witness.
        outState.putParcelable(STATE_WITNESS, witness);
    }
}
